package com.itheima.web3.web;

import com.alibaba.fastjson2.JSON;
import com.itheima.web3.pojo.User;
import com.itheima.web3.service.impl.UserServiceImpl;
import com.itheima.web3.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/v1/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //告诉tomcat解码用utf-8解
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        System.out.println(user);
        UserServiceImpl userService = new UserServiceImpl();
        User user1 = userService.login(user);
        if(user1!=null) {
            resp.getWriter().write(JSON.toJSONString(Result.ok(user1.getUsername()+"登录成功")));
        }else {
            resp.getWriter().write(JSON.toJSONString(Result.error("账户不存在，请重新登录")));
        }
    }
}
